const Mock = require('mockjs')

Mock.mock('@SUBJECTNAME')
Mock.mock('@IMAGE')
// const subjectid =
const listData = Mock.mock({
  'list|88': [
    {
      'id|+1': 1, // 词汇id
      description: '@ctitle(6)', // 词汇描述
      wordName: '@title(1)', // 词汇名称
      subject: '@SUBJECTNAME',
      'isCollect|0-1': 1, // 序号
      'wordHot|10-45': 1, // 词汇热度
      name: '@cname(4)', // 创建者
      createTime: '@date(yyyy-MM-dd HH:mm:ss)' // 时间
    }
  ]
})
// console.log(listData)
module.exports = [
  // 列表，搜索功能
  {
    url: '/mock/content/word',
    type: 'get',
    response: (config) => {
      // console.log(JSON.parse(config.query.subject))
      if (config.query.id) {
        // console.log(listData.list)
        const res = listData.list.find(item => item.id * 1 === config.query.id * 1)
        // console.log(res)
        if (res) {
          return {
            code: 0, message: '', data: res }
        } else {
          return { code: 9997, message: '数据不存在', data: res }
        }
      } else {
        let { page = 1, limit = 10 } = config.query
        const { wordName, description, name, subject } = config.query
        page = page * 1
        limit = limit * 1
        let clist = JSON.parse(JSON.stringify(listData.list))
        // console.log(clist)
        if (wordName) {
          console.log('wordName')
          clist = clist.filter(item => item.wordName === wordName)
          total = clist.length
        }
        if (description) {
          console.log('description')
          clist = clist.filter(item => item.description === description)
          total = clist.length
        }
        if (name) {
          console.log('name')
          clist = clist.filter(item => item.name === name)
          total = clist.length
        }
        if (JSON.parse(subject).subjectId) {
          console.log('subjectId', JSON.parse(subject).subjectId)
          clist = clist.filter(item => item.subject.subjectId * 1 === JSON.parse(subject).subjectId * 1)
          total = clist.length
        }
        let total = clist.length
        const list = clist.splice((page - 1) * limit, limit)
        return {
          code: 0,
          message: 'ok',
          data: {
            total,
            list
          }
        }
      }
    }
  },

  // 增
  {
    url: '/mock/content/word',
    type: 'post',
    response: (config) => {
      console.log(config)
      const { body } = config
      // if (!body.tagname) {
      //   return { code: 9997, message: '必填项不能是空的', data: '' }
      // }
      body.id = listData.list[listData.list.length - 1].id + 1
      listData.list.unshift(body)
      return { code: 0, message: '成功', data: '' }
    }
  },
  // 改
  {
    url: '/mock/content/word',
    type: 'put',
    response: (config) => {
      // console.log(config)
      const { body } = config
      if (!body.id) {
        return { code: 9997, message: '请提交ID', data: '' }
      }
      const index = listData.list.findIndex(
        item => item.id * 1 === body.id * 1
      )
      if (index === -1) {
        return { code: 9997, message: '无数据', data: '' }
      }
      listData.list[index] = body
      return { code: 0, message: '成功', data: '' }
    }
  },
  // 删
  {
    url: '/mock/content/word',
    type: 'delete',
    response: (config) => {
      // console.log(config)
      if (!config.query.id) {
        return { code: 9997, message: '请提交ID', data: '' }
      }
      const index = listData.list.findIndex(
        item => item.id * 1 === config.query.id * 1
      )
      if (index === -1) {
        return { code: 9997, message: '无数据', data: '' }
      }
      listData.list.splice(index, 1)
      return { code: 0, message: '成功', data: '' }
    }
  }
]
